package com.yy.common.utils.video;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * ClassName: Mp4mergeUtils
 * Package: com.yy.common.utils
 * Description:
 *
 * @Author yy
 * @Create 2023-12-11 19:51
 * @Version 1.0
 */
public class Mp4mergeUtils {

    public static void merge2TargetFileDeleteSourceFile(String targetFilePath,
                                                        ArrayList<String> sourceFilePathList) {
        // 如果ArrayList中有东西
        if (sourceFilePathList.size() > 0) {
            BufferedInputStream in;
            try (BufferedOutputStream out = new BufferedOutputStream(
                    new FileOutputStream(new File(targetFilePath)));) {
                System.out.println("源文件列表:");
                for (Iterator<String> iterator = sourceFilePathList
                        .iterator(); iterator.hasNext(); ) {
                    String sourceFilePath = iterator.next();
                    System.out.println("    " + sourceFilePath);
                    File sourceFile = new File(sourceFilePath);
                    in = new BufferedInputStream(
                            new FileInputStream(sourceFile));
                    // 缓存数组
                    byte[] buffer = new byte[2048];
                    // 每次读入的字节数量
                    int inSize = -1;
                    // 批量读入字节到buffer缓存中,并返回读入的自己数量给inSize
                    while ((inSize = in.read(buffer)) != -1) {
                        // 把buffer缓存中的字节写入输出流(也就是目标文件)
                        out.write(buffer, 0, inSize);
                    }
                    // 关闭源文件
                    in.close();
                    // 删除这个源文件
                    sourceFile.delete();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}


